package com.springsecurity.service.impl;

import com.springsecurity.mapper.Tusermapper;
import com.springsecurity.pojo.TUser;
import com.springsecurity.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author cc
 * @date 2021/9/4 10:03
 * @Description
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private Tusermapper tusermapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        TUser finduserbyusername = tusermapper.finduserbyusername(username);
        if (finduserbyusername!=null){
            List<SimpleGrantedAuthority> simpleGrantedAuthorities = new ArrayList<>();
            simpleGrantedAuthorities.add(new SimpleGrantedAuthority("ROLE_USER"));
            return new User(finduserbyusername.getUsername(),finduserbyusername.getPassword(),simpleGrantedAuthorities);
        }
        return null;
    }
}
